package com.dcbeida.activity.zyff;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.CaptureActivity;
import com.dcbeida.activity.applyreturn.ApplyReturnActivity;
import com.dcbeida.activity.hospital.HospitalActivity;
import com.dcbeida.adapter.SalsesDetail2Adapter;
import com.dcbeida.adapter.SalsesDetailRYAdapter;
import com.dcbeida.adapter.ZyDetail2Adapter;
import com.dcbeida.bean.CardEnum;
import com.dcbeida.bean.DrguItemDetail;
import com.dcbeida.bean.DrguItemDetailWithHeader;
import com.dcbeida.bean.DrugCodeInfo;
import com.dcbeida.bean.Sales;
import com.dcbeida.bean.SignStatusEnum;
import com.dcbeida.bean.ZyDrugDetail;
import com.dcbeida.utils.Constants;
import com.dcbeida.widget.ListViewForScrollView;
import com.dcbeida.widget.scanitem.DetailItemView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import butterknife.Bind;
import butterknife.OnClick;

public class ZyWithGaDetailActivity extends BaseActivity implements ZyWithGaDetailContract.View {

    @Bind(R.id.img_back)
    ImageView img_back;

    @Bind(R.id.tv_title)
    TextView tv_title;

    @Bind(R.id.tv_right)
    TextView tv_right;
    @Bind(R.id.tv_name)
    TextView tv_name;
    @Bind(R.id.tv_dh)
    TextView tv_dh;
    @Bind(R.id.tv_sfz)
    TextView tv_sfz;
    @Bind(R.id.tv_yy_name)
    TextView tv_yy_name;
    @Bind(R.id.tv_money)
    TextView tv_money;
    @Bind(R.id.tv_bh)
    TextView tv_bh;
    @Bind(R.id.tv_time)
    TextView tv_time;
    @Bind(R.id.lin_pics)
    LinearLayout lin_pics;
    @Bind(R.id.lin_zy1)
    LinearLayout lin_zy1;
    @Bind(R.id.lin_other_type)
    LinearLayout lin_other_type;
    @Bind(R.id.lin_zy2)
    LinearLayout lin_zy2;
    @Bind(R.id.tv_v2_memo)
    TextView tv_v2_memo;
    @Bind(R.id.dl_item_add)
    DetailItemView dl_item_add;
    @Bind(R.id.lin_gajh)
    LinearLayout lin_gajh;
    @Bind(R.id.iv_go)
    ImageView iv_go;
    @Bind(R.id.tv_qh)
    TextView tv_qh;
    @Bind(R.id.tv_sqth)
    TextView tv_sqth;
    @Bind(R.id.tv_count)
    TextView tv_count;
    @Bind(R.id.tv_detail)
    TextView tv_detail;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;

    @Bind(R.id.myList)
    ListViewForScrollView myList;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.tv_sign_status)
    TextView tv_sign_status;

    @Bind(R.id.tv_doctor)
    TextView tv_doctor;


    private ZyWithGaDetailContract.Present mPresent;

    private String orderId;
    private String parentid;
    private Sales.ListBean bean;
    private ZyDetail2Adapter adapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.zy_with_ga_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initParam();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey("orderId")) {
            orderId = bundle.getString("orderId");
        }
        if (bundle != null && bundle.containsKey("parentid")) {
            parentid = bundle.getString("parentid");
        }
        if (bundle != null && bundle.containsKey("Sales.ListBean")) {
            bean = (Sales.ListBean) bundle.getSerializable("Sales.ListBean");
        }
        super.onCreate(savedInstanceState);
        initView();


    }

    private void initParam() {
        mPresent = new ZyWithGaDetailPresenter(this, this);
    }

    private void initView() {
        tv_title.setText("销售详情");
        tv_right.setVisibility(View.GONE);
        img_back.setVisibility(View.VISIBLE);
        if (bean == null)
            throw new IllegalArgumentException("页面丢失信息，检查");
        tv_name.setText(bean.getTruename());
        tv_dh.setText(getString(R.string.lxfs) + bean.getPhonenum() + "");

        String cardName = CardEnum.getCardName(bean.getCardtype());
        tv_sfz.setText(cardName+":"  + bean.getIdcardnum() + "");
        tv_doctor.setText("医生:"+bean.getDoctorname());
        tv_sign_status.setText("知情书签署情况:"+ SignStatusEnum.getCardName(bean.getSignstatus()));
        tv_yy_name.setText(bean.getHospitalname());
        tv_money.setText("￥" + bean.getAmount());
        tv_bh.setText("编号:" + bean.getId());
        tv_time.setText(bean.getCreatetime());
    }

    @Override
    public void setData() {
        super.setData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresent.getorderdrugsv2(orderId, parentid);
    }

    @OnClick({R.id.img_back, R.id.lin_zy1, R.id.tv_sqth})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.lin_zy1:
                if (dl_item_add.getVisibility() == View.VISIBLE) {
                    dl_item_add.setVisibility(View.GONE);
                    iv_go.setImageResource(R.drawable.more);
                } else {
                    dl_item_add.setVisibility(View.VISIBLE);
                    iv_go.setImageResource(R.drawable.down);
                }
                break;
            case R.id.tv_sqth:
                Bundle bundle = new Bundle();
                bundle.putSerializable("Sales.ListBean", bean);
                showActivityForResult(ApplyReturnActivity.class, bundle, Constants.ONE);
                break;
        }
    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void showSuccess(ZyDrugDetail detail) {
        if (detail.getType() == 1) {
            lin_pics.setVisibility(View.GONE);
            lin_zy1.setVisibility(View.VISIBLE);
            tv_sqth.setVisibility(View.VISIBLE);
            List<DrguItemDetail> mList = new ArrayList<>();
            int id = 0;
            if(detail.getOrignaldruglist() != null && detail.getOrignaldruglist().size()>0) {
                dl_item_add.removeAllViews();
                for (DrguItemDetailWithHeader header : detail.getOrignaldruglist()) {
                    id++;
                    for (DrguItemDetail itemDt : header.getDrugList()) {
                        itemDt.setAllQuantity(header.getQuantity());
                        itemDt.setDrugname(header.getDrugname());
                        itemDt.setId(id);
                        dl_item_add.addData(itemDt);
                        mList.add(itemDt);
                    }

                }
            }

        } else if (detail.getType() == 2) {
            if (detail.getCoupon() == null)
                return;
            lin_pics.setVisibility(View.VISIBLE);
            lin_zy2.setVisibility(View.VISIBLE);
            lin_gajh.setVisibility(View.VISIBLE);

            if(getString(R.string.bjga).equals(detail.getCoupon().getCoupontype())){
                tv_v2_memo.setText("关联"+(getString(R.string.bjga))+"赠药券");
            }else if(getString(R.string.yxbl).equals(detail.getCoupon().getCoupontype())){
                tv_v2_memo.setText("关联"+getString(R.string.yxbl)+"赠药券");
                lin_other_type.setVisibility(View.GONE);
            }else  if(getString(R.string.shfz).equals(detail.getCoupon().getCoupontype())){
                tv_v2_memo.setText("关联"+getString(R.string.shfz)+"赠药券");
                lin_other_type.setVisibility(View.GONE);
            }else {
                tv_v2_memo.setText("关联"+detail.getCoupon().getCoupontype()+"赠药券");
                lin_other_type.setVisibility(View.GONE);
            }
            tv_qh.setText("券号:" + detail.getCoupon().getCouponcode());
            tv_count.setText(detail.getCoupon().getUsetimes() + "");
            tv_detail.setText("核销药店：" + detail.getCoupon().getShopname() + "\n核销时间：" + detail.getCoupon().getUsetime());
            setPicList(detail.getCoupon().getRecipepics());


        }

        scrollView.smoothScrollTo(0, 0);
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        adapter = new ZyDetail2Adapter(this);
        myList.setAdapter(adapter);

        boolean isUnRefiund=false;
        if (detail.getList() == null)
            return;
        List<ZyDrugDetail.ListDTO.DrugListDTO> mList = new ArrayList<>();
        for (ZyDrugDetail.ListDTO listDTO : detail.getList()) {
            int k = 0;
            for (ZyDrugDetail.ListDTO.DrugListDTO drugListDTO : listDTO.getDrugList()) {
                if (k == 0) {
                    drugListDTO.setFirst(true);
                }
                drugListDTO.setAllQuantity(listDTO.getQuantity());
                drugListDTO.setDrugname(listDTO.getDrugname());
                mList.add(drugListDTO);
                k++;
                if("0".equals(drugListDTO.getRefundid())){
                    isUnRefiund =true;
                }
            }

        }
        if(detail.getType() == 1) {
            if (isUnRefiund) {
                tv_sqth.setVisibility(View.VISIBLE);
            } else {
                tv_sqth.setVisibility(View.GONE);
            }
        }
        adapter.setmArrayList(mList);
    }

    @Override
    public void setPresenter(ZyWithGaDetailContract.Present presenter) {

    }

    private void setPicList(String picList) {
        ArrayList<String> paths = new ArrayList<>();
        if (!TextUtils.isEmpty(picList)) {
            recyclerview.setVisibility(View.VISIBLE);
            if (picList.contains(",")) {
                String[] imgUrl = picList.split(",");
                for (int i = 0; i < imgUrl.length; i++) {
                    paths.add(imgUrl[i]);
                }
            } else {
                paths.add(picList);
            }
        } else {
            lin_pics.setVisibility(View.GONE);
            recyclerview.setVisibility(View.GONE);
            return;
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerview.setHasFixedSize(true);
        SalsesDetailRYAdapter ryAdapter = new SalsesDetailRYAdapter(mContext);
        recyclerview.setAdapter(ryAdapter);
        ryAdapter.setData(paths);

//        scrollView.smoothScrollTo(0, 0);
//        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
//        myList.setOnItemClickListener(this);
//        adapter = new SalsesDetail2Adapter(this);
//        myList.setAdapter(adapter);
    }
}
